using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using DataObject.Core;
using DataObject.Database;

namespace DataObject.DataAccess
{
    public partial class AgentPricesDAO : DataBaseProcessBase<AgentPricesEntity, OBSDataContext>
    {

        #region CreateInstant
        public static AgentPricesDAO CreateInstant()
        {
            return new AgentPricesDAO();
        }
        #endregion

        public List<AgentPricesEntity> GetAllPrices()
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.AgentPricesEntities.ToList();
            }
        }

        public List<AgentPricesEntity> GetPrices(string agentID)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.AgentPricesEntities.Where(a => a.AgentID == agentID).ToList();
            }
        }

        public List<AgentPricesEntity> FindPrice(
            System.Guid? id,
            string agentID,
            string priceType,
            int? typeOfCabin,
            int? route,
            string boat,
            long? price,
            HashSet<string> sortClause)
        {
            int count = 0;
            return FindPrice(id, agentID, priceType, typeOfCabin, route, boat, price, null, null, out count, sortClause);
        }

        public List<AgentPricesEntity> FindPrice(
            System.Guid? id,
            string agentID,
            string priceType,
            int? typeOfCabin,
            int? route,
            string boat,
            long? price,
            int? page,
            int? row,
            out int count,
            HashSet<string> sortClause)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from table in db.AgentPricesEntities select table);
                if (id.HasValue)
                    query = query.Where(table => table.Id == id.Value);
                if (!agentID.Equals(string.Empty))
                    query = query.Where(table => table.AgentID.ToLower().Equals(agentID.ToLower()));
                if (!priceType.Equals(string.Empty))
                    query = query.Where(table => table.PriceType.ToLower().Equals(priceType.ToLower()));
                if (typeOfCabin.HasValue)
                    query = query.Where(table => table.TypeOfCabin == typeOfCabin.Value);
                if (route.HasValue)
                    query = query.Where(table => table.Route == route.Value);
                if (!boat.Equals(string.Empty))
                    query = query.Where(table => table.Boat.ToLower().Equals(boat.ToLower()));
                if (price.HasValue)
                    query = query.Where(table => table.Price == price.Value);
                for (int i = sortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(sortClause.ElementAt(i));
                count = query.Count();
                if (page.HasValue && row.HasValue)
                {
                    return query.PageIQuery(page.Value, row.Value).ToList();
                }
                return query.ToList();
            }
        }

        public AgentPricesEntity GetAgentPrice(System.Guid id)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.AgentPricesEntities.SingleOrDefault(a => a.Id == id);
            }
        }

        public AgentPricesEntity GetAgentPrice(string PriceType, string agentID, int routeID, int typeOfCabin, string boatid)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.AgentPricesEntities.SingleOrDefault(a => a.PriceType == PriceType
                    && a.AgentID == agentID
                    && a.Route == routeID
                    && a.TypeOfCabin == typeOfCabin
                    && a.Boat.Equals(boatid));
            }
        }

        public void InsertPrices(List<AgentPricesEntity> prices)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.AgentPricesEntities.InsertAllOnSubmit(prices);
                db.SubmitChanges();
            }
        }

        public void UpdatePrice(AgentPricesEntity price)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                // Call Store Procedure.
                db.UpdateAgentPrice(price.Id, price.AgentID, price.PriceType, price.TypeOfCabin, price.Route, price.Boat, price.Price);
            }
        }

    }
}
